
from fastapi import Form
from src.core.session import *
from src.entity.models import Group, UpdateGroup, CreateGroup, BasicModel
from src.core.logconfig import log
from src.common.response import success_response, fail_response
from src.common.status import CLASS_STATUS
from src.utils.tools import get_cur_time
from src.common.base_router import BaseRouter
from src.common.base_service import BaseService

# 使用基础路由类创建路由
group = BaseRouter(
    entity=Group,
    create_schema=CreateGroup,
    update_schema=UpdateGroup,
    prefix="/sys/group",
    tags=["Group"]
).router

log = log.logger
sc = SQLCON
# 创建服务实例
group_service = BaseService(Group)

async def query_all():
    return group_service.get_list()


async def query_one(id):
    return group_service.get_by_id(id)

async def query_data(id):
    return group_service.get_list_filter(id)

async def create(name:str=Form(), desc:str=Form(default=None)):
    create_data = CreateGroup(name=name, desc=desc, status=1, create_time=get_cur_time())
    return group_service.create(create_data)


async def update(id: int=Form(),name: str=Form(), status: int = Form(default=1), desc: str = Form(default=None)):
    update_data = UpdateGroup(id=id, name=name, status=status, desc=desc, update_time=get_cur_time())
    return group_service.update(update_data)

async def query_one(data: BasicModel):
    # 更新数据为删除状态
    delete_data = UpdateGroup(
        id=data.id,
        status=CLASS_STATUS["STATE_DELETED"],
        update_time=get_cur_time())
    return group_service.update(delete_data)
